﻿using System;
using System.Web;
using System.Linq;
using System.Web.SessionState; //第一步：导入此命名空间
//ashx文件要使用Session,必须实现Session接口;
using System.Collections;

using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using iMaker.Library;
using HKEA.CMS.Core.BLL.Tbl;

namespace WebApplication
{
    /// <summary>
    /// Login_Processor 的摘要说明
    /// </summary>
    public class Login_Processor : IHttpHandler, IRequiresSessionState //第二步：实现接口 到此就可以使用Session了
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            if (context.Request["type"] == "login")
                context.Response.Write(LoginRequest(context));
            else if (context.Request["type"] == "forget_passwd")
                context.Response.Write(ForgetPassWord(context));
        }

        protected string LoginRequest(HttpContext context)
        {
            string msg = string.Empty;

            tblUser ent = new tblUser();
            tblUserBLL bllUser = tblUserBLL.GetInstance();
            ent = bllUser.UserAuth(context.Request["account"],context.Request[ "passwd"],ref msg);
            if (ent != null)
            {
                context.Session[Constant.SessionName.Manage] = ent;
                msg = "1";
            }
            return msg;
        }

        protected string ForgetPassWord(HttpContext context)
        {
            string msg = string.Empty;

            tblUser ent = new tblUser();
            tblUserBLL bllUser = tblUserBLL.GetInstance();
            ent.User_Email = context.Request["email"];
            ent = bllUser.GetENT(ent);
            if (ent != null)
            {
                string new_passwd = iMaker.Library.StringSecurity.GetRnd(8, true, true, false, false, "hkea");
                ent.User_Password = new_passwd;

                //发送Email
                Hashtable args=new Hashtable();
                args.Add("user_name", ent.User_Name);
                args.Add("datetime", DateTime.Now.ToString("yyyy-MM-dd hh:mm"));
                args.Add("user_password", new_passwd);
                MailManage.ForgetPassword("EmailSubject_ForgetPasswd", Constant.EmailTemplateName.ForgetPasswd, ent.User_Email, args);

                //修改密码
                int count = bllUser.UpdateENT(ent);

                msg = ResourceManage.GetResourceString("msg100000");//"密码重置成功，查收您的电邮.";
            }
            else
                msg = ResourceManage.GetResourceString("msg100001");//"该电邮没有相关的账号关联.";

            return msg;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}